from urllib import request,error
import re,gzip

try:
    url = 'https://bj.58.com/chuzu/?PGTID=0d100000-0000-1f74-fcd6-8ee31f00971e&ClickID=3'

    headers ={
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
        }

    req = request.Request(url=url,headers=headers)
    res = request.urlopen(req)

    html = res.read().decode('utf-8')
    pat='<li logr=.*?>.*?<img.*? lazy_src="(.*?)".*?>.*?<a href="(.*?)".*?class="strongbox".*?>(.*?)</a>.*?<p class="room strongbox">(.*?)</p>.*?<p class="add">.*?<a .*?>(.*?)</a>.*?<a.*?>(.*?)</a>.*?<b class="strongbox">(.*?)</b>'
    dlist = re.findall(pat,html,re.S)

    print(len(dlist))

    for v in dlist:
        print("租房信息展示：")
        print("title:"+v[2].strip())
        print("area:"+v[3].replace(" ","").replace("&nbsp;",""))
        print("location:"+v[4])
        print("price:"+v[6]+"元/月")
        print("picture:"+v[0])
except Exception as e:
    print(e)
    if hasattr(e,"code"):
        print("HTTPError:"+e.reason)
        print(e.code)
    elif hasattr(e,"reason"):
        print("URLError:"+e.reason)
